package com.qianfeng.zhangwei.camera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.qianfeng.zhangwei.camera.utils.FileUtils;

import java.io.File;

/**
 * 1> 调用系统相机  拍照
 * 2> 多选  多文件上传
 * 3> 图片的压缩技术
 * 流程:拍照---->>选择图片---->> 上传服务器
 * 一拍照:
 * 第一种调用系统相机保存在默认文件夹中
 * 第二种 调用系统相机制定保存的文件夹
 */

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    @ViewInject(R.id.photo_btn)
    private Button photoBtn;
    @ViewInject(R.id.photo_tofile_btn)
    private Button photoToFileBtn;
    @ViewInject(R.id.upload_btn)
    private Button uploadBtn;


    @ViewInject(R.id.show_image)
    private ImageView img;


    public static final int PHOTO_REQUEST_CODE = 989;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);

    }

    @OnClick({R.id.photo_btn, R.id.photo_tofile_btn, R.id.upload_btn})
    public void onCLick(View view) {
        switch (view.getId()) {
            case R.id.photo_btn:
                defaultPhoto();
                break;
            case R.id.photo_tofile_btn:
                photoToFile();
                break;
            case R.id.upload_btn:
                Intent intent = new Intent(this, UploadActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 拍照保存指定的文件夹
     */
    private void photoToFile() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //  文件
        File imageFile = FileUtils.getImageFile();
        if (imageFile != null) {
            Uri uri = Uri.fromFile(imageFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivity(intent);
        } else {
            Toast.makeText(this, "请检查Sdk是否存在", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 调用系统默认的相机 保存在默认的文件夹中
     */
    private void defaultPhoto() {
        //MediaStore.ACTION_VIDEO_CAPTURE  录像
        //ACTION_IMAGE_CAPTURE 拍照
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, PHOTO_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PHOTO_REQUEST_CODE) {
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            if (bitmap != null) {
                img.setImageBitmap(bitmap);
            }

        }
    }

    //
//    private void testJpeg() {
//        new Thread(new Runnable() {
//            public void run() {
//                try {
//                    int quality = 90;
//                    InputStream in = getResources().getAssets()
//                            .open("test.jpg");
//                    Bitmap bit = BitmapFactory.decodeStream(in);
//                    File dirFile = getExternalCacheDir();
//                    if (!dirFile.exists()) {
//                        dirFile.mkdirs();
//                    }
//                    File originalFile = new File(dirFile, "original.jpg");
//                    FileOutputStream fileOutputStream = new FileOutputStream(
//                            originalFile);
//                    bit.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
//                    File jpegTrueFile = new File(dirFile, "jpegtrue.jpg");
//                    File jpegFalseFile = new File(dirFile, "jpegfalse.jpg");
//                    NativeUtil.compressBitmap(bit, quality,
//                            jpegTrueFile.getAbsolutePath(), true);
//                    NativeUtil.compressBitmap(bit, quality,
//                            jpegFalseFile.getAbsolutePath(), false);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        }).start();
//    }
}
